use std::sync::Arc;
use std::thread;
use std::time::Duration;
use crate::concurrent_engine::{ConcurrentEngine, ConcurrentCounter};

fn main() {
    // 创建并发引擎和计数器
    let engine = ConcurrentEngine::new(4);
    let counter = Arc::new(ConcurrentCounter::new(0));
    
    // 提交一些任务
    println!("提交任务...");
    for i in 0..10 {
        let counter = counter.clone();
        let task_id = i;
        engine.submit_task(move || {
            println!("执行任务 {}", task_id);
            counter.increment();
            thread::sleep(Duration::from_millis(100)); // 模拟工作
        });
    }
    
    // 等待任务完成
    println!("等待任务完成...");
    let start_time = std::time::Instant::now();
    let timeout = Duration::from_secs(5);
    
    loop {
        let count = counter.get();
        let tasks_remaining = engine.task_queue.size.load(std::sync::atomic::Ordering::Relaxed);
        println!("已完成任务数: {}, 剩余任务数: {}", count, tasks_remaining);
        
        if tasks_remaining == 0 {
            break;
        }
        
        if start_time.elapsed() > timeout {
            println!("超时，任务未完成。剩余任务数: {}", tasks_remaining);
            break;
        }
        
        thread::sleep(Duration::from_millis(200));
    }
    
    // 关闭引擎
    println!("关闭引擎...");
    engine.shutdown();
    println!("测试完成。最终计数器值: {}", counter.get());
}